package com.mmall.service.Impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;


@Service("iCategoryService")
@Slf4j
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public ServerResponse<List<Integer>> getSelfAndChildrenByCategoryId(Integer categoryId) {

        Set<Category> categorySet = Sets.newHashSet();
        findChildrenCategory(categoryId, categorySet);

        List<Integer> categoryIdList = Lists.newArrayList();
        if (categorySet != null) {
            for (Category category : categorySet) {
                categoryIdList.add(category.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryIdList);
    }


    public Set<Category> findChildrenCategory(Integer categoryId, Set<Category> categorySet) {
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if (category != null) {
            categorySet.add(category);
        }

        //查找子节点
        List<Category> categoryList = categoryMapper.selectChildrenByCategoryId(categoryId);
        for (Category categoryItem : categoryList) {
            findChildrenCategory(categoryItem.getId(), categorySet);
        }
        return categorySet;
    }

    public ServerResponse getChildrenCategoryPoint(Integer categoryId) {
        if (categoryId == null) {
            return ServerResponse.createBySuccess();
        }
        List<Category> categoryList = categoryMapper.selectByParentId(categoryId);
        if (CollectionUtils.isEmpty(categoryList)) {
            log.info("获取子种类为空");
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createBySuccess(categoryList);
    }


    public ServerResponse addCategory(String categoryName, Integer parentId) {
        if (categoryName == null || parentId == null) {
            return ServerResponse.createByErrorMessage("添加种类参数错误");
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);//默认种类可用

        int count = categoryMapper.insertSelective(category);
        if (count > 0) {
            return ServerResponse.createBySuccessMessage("添加种类成功");
        }
        return ServerResponse.createBySuccessMessage("添加种类失败");

    }


    public ServerResponse setCategoryName(Integer categoryId, String categoryName) {
        if (categoryName == null || categoryId == null) {
            return ServerResponse.createBySuccessMessage("更新种类名称参数错误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);

        int count = categoryMapper.updateByPrimaryKeySelective(category);
        if (count > 0) {
            return ServerResponse.createBySuccessMessage("更新种类名称成功");
        }
        return ServerResponse.createBySuccessMessage("更新种类名称失败");

    }
}

